//
//  FinderCell.m
//  Atlantis
//
//  Created by 何恩营 on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FinderCell.h"
#import "CategoryCollectionCell.h"
#import "PlayViewController.h"
#import "AVPlayer+player.h"
#import "UIColor+AddColor.h"

#define REUSE @"reuse"

@interface FinderCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain) UIView *view;
// 分类标题
@property(nonatomic, retain) UILabel *titleLabel;
// 访问更多
@property(nonatomic, retain) UILabel *moreLabel;
// flowLayout
@property(nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, assign) NSInteger flag;
// 播放器
@property(nonatomic, retain) AVPlayer *avplay;
@end

@implementation FinderCell

// 属性的内存管理
- (void)dealloc
{
    [_view release];
    [_titleLabel release];
    [_moreLabel release];
    [_collectionView release];
    [_hotSounds release];
    [_flowLayout release];
    [_avplay release];
    [super dealloc];
}

// 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        // 创建UIView
        self.view = [[UIView alloc] init];
        
        [self.contentView addSubview:self.view];
        [_view release];
        
        // 创建标题Label
        self.titleLabel = [[UILabel alloc] init];
        // 将titleLabel添加到视图控制器自带的视图上
        [self.view addSubview:self.titleLabel];
        [_titleLabel release];
        
        // 创建更多Label
        self.moreLabel = [[UILabel alloc] init];
        // 添加到系统VIew上
        [self.view addSubview:self.moreLabel];
        [_moreLabel release];
        
        // UICollectionViewFlowLayout 是一个layout类的基类, 不能直接使用, 它只是规定了一些必须要重写的方法和属性
         self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        // 创建一个集合视图
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:self.flowLayout];
        [_flowLayout release];
        [_collectionView release];
        
    }
    return self;
}

// 重写HotSounds的setter方法
- (void)setHotSounds:(HotSoundsCategories *)hotSounds
{
    // 判断传进来的参数是否跟之前的为同一个
    if (_hotSounds != hotSounds) {
        // 不相同, 先释放之前的
        [_hotSounds release];
        // 然后将参数赋给hotSounds变量
        _hotSounds = [hotSounds retain];
    }
    // 设置titleLabel对齐方式为 左对齐
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    // 给titleLabel赋值
    self.titleLabel.text = self.hotSounds.title;
    // 
    self.moreLabel.textAlignment = NSTextAlignmentRight;
    self.moreLabel.text = @"更多>>";
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.view.frame = CGRectMake(0, 10, self.contentView.bounds.size.width, 30);
    self.titleLabel.frame = CGRectMake(30, 0, 100, 30);
    self.moreLabel.frame = CGRectMake(self.view.frame.size.width - 80, 0, 70, 30);
    // 不同类别的音乐列表(ColletionView)
    
    // collectionView所有的cell的位置都不是固定的, 是由layout对象决定
    
    // UICollectionViewFlowLayout 是一个layout类的基类, 不能直接使用, 它只是规定了一些必须要重写的方法和属性
   
    self.flowLayout.itemSize = CGSizeMake(self.contentView.bounds.size.width / 3, self.contentView.bounds.size.height - 40);
    
    //NSLog(@"%f", self.flowLayout.itemSize.width);
    // 设置行间距为0
    self.flowLayout.minimumLineSpacing = 0;
    // 设置列间距为0
    self.flowLayout.minimumInteritemSpacing = 0;
    // 设置集合视图的滚动方向
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    // 创建集合视图
    self.collectionView.frame = CGRectMake(0, 40, self.contentView.bounds.size.width, self.contentView.bounds.size.height - 40);
    // 指定代理人
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    // 将集合视图的背景颜色设置为 #e6e6e6 编码代表的颜色
    self.collectionView.backgroundColor = [UIColor colorFromHexCode:@"#e6e6e6"];
    self.avplay = [AVPlayer shareAVPlayer];
   // NSLog(@"%f", self.contentView.frame.size.width - self.collectionView.frame.size.width);
    
    // 将集合视图添加到大scrollView上
    [self.contentView addSubview:self.collectionView];
    
    // collectionView必须要注册一个cell类
    [self.collectionView registerClass:[CategoryCollectionCell class] forCellWithReuseIdentifier:REUSE];

    
    self.collectionView.pagingEnabled = YES;
   // self.collectionView.showsHorizontalScrollIndicator = NO;
}

#pragma mark collectionView协议方法
// cell的行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.hotSounds.soundsArr.count;
}
// cell的值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:REUSE forIndexPath:indexPath];
       self.flag = 0;
    Music *music = [self.hotSounds.soundsArr objectAtIndex:indexPath.row];
    cell.music = music;
    return cell;
}
// collectionView已经选择项
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    [self.delegate push:self.hotSounds.soundsArr index:indexPath.row];
    
}

// collectionView的选择项
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//// 选中item执行的方法
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
//    PlayViewController *playVC = [[PlayViewController alloc] init];
//    playVC.sound = [self.hotSounds.soundsArr objectAtIndex:indexPath.row];
//}



- (void)awakeFromNib {
    // Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
